#include "TwOgreBoolVariable.h"

#include <sstream>

#include "TwOgreWindow.h"
#include "TwOgreWindowManager.h"

namespace TwOgre{
	BoolVariable::BoolVariable(Window *parent, const Ogre::String& name, bool readOnly, const Ogre::String& label, 
		bool value, const Ogre::String& groupName, const Ogre::String& help) :
		Variable(parent, name, TW_TYPE_BOOLCPP), mValue(value)
	{
		if(readOnly)
		{
			TwAddVarRO(mParent->getTweakBar(), mName.c_str(), TW_TYPE_BOOLCPP, &mValue, "");
		}
		else
		{
			TwAddVarRW(mParent->getTweakBar(), mName.c_str(), TW_TYPE_BOOLCPP, &mValue, "");
		}

		if(label.compare("") != 0)
		{
			setLabel(label);
		}
		if(groupName.compare("") != 0)
		{
			setGroup(groupName);
		}
		if(help.compare("") != 0)
		{
			setHelp(help);
		}
	}

	BoolVariable::BoolVariable(BoolParams& params) :
		Variable(params.parent_, params.name_, params.type_), mValue(params.value_)
    {
        if((params.getCB_ != NULL) && (params.setCB_ != NULL) && (params.data_ != NULL))
        {
            TwAddVarCB(mParent->getTweakBar(), mName.c_str(), params.type_, params.setCB_, params.getCB_, params.data_, "");
        }
        else
        {
            if(params.readOnly_)
		    {
                TwAddVarRO(mParent->getTweakBar(), mName.c_str(), params.type_, &mValue, "");
		    }
		    else
		    {
                TwAddVarRW(mParent->getTweakBar(), mName.c_str(), params.type_, &mValue, "");
		    }
        }
        
        if(params.label_.compare("") != 0)
		{
			setLabel(params.label_);
		}
        if(params.group_.compare("") != 0)
		{
            setGroup(params.group_);
		}
        if(params.help_.compare("") != 0)
		{
            setHelp(params.help_);
		}
        if(params.shortcut_.compare("") != 0)
		{
            setShortcut(params.shortcut_);
		}
        if((params.strTrue_.compare("") != 0) && (params.strFalse_.compare("") != 0))
        {
            setDisplayStrings(params.strTrue_, params.strFalse_);
        }
    }
	
    BoolVariable::~BoolVariable()
	{
		TwRemoveVar(mParent->getTweakBar(), mName.c_str());
	}

	void BoolVariable::setDisplayStrings(const Ogre::String& trueString, const Ogre::String& falseString)
	{
		std::stringstream ss;
		ss << " '" << mParent->getName() << "'/'" << mName << "' True='" << trueString.c_str() << "' False='" << falseString << "' ";

		TwDefine(ss.str().c_str());
	}

	void BoolVariable::setShortcut(const Ogre::String& toggle)
	{
		std::stringstream ss;
		ss << " '" << mParent->getName() << "'/'" <<  mName 
		   << "' Key='" << toggle << "' ";

		TwDefine(ss.str().c_str());
	}
}; // End Namespace
